package com.googlecode.scrptingdoclet.evaluator.impl;

import javax.script.ScriptEngine;
import javax.script.ScriptException;

import com.googlecode.scrptingdoclet.evaluator.Evaluator;
import com.googlecode.scrptingdoclet.evaluator.annotation.ScriptEvaluator;
import com.googlecode.scrptingdoclet.model.DefaultClassModel;
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaField;
import com.thoughtworks.qdox.model.JavaMethod;

@ScriptEvaluator(engine = "ruby", precedence = ScriptEvaluator.BUILT_IN)
public class DefaultRubyEvaluator implements Evaluator<DefaultClassModel> {

	private ScriptEngine engine;

	public void setScriptEngine(ScriptEngine engine) {
		this.engine = engine;
	}

	public DefaultClassModel evalClass(JavaClass clazz, String script)
			throws ScriptException {

		System.out.println("script=" + script);

		Object eval = engine
				.eval("def name(args)\n puts 'hello:' + args.to_s\nend\n"
						+ "def ref_example(args)\n puts 'ref:' + args.to_s\nend\n"
						+ script);

		DefaultClassModel model = new DefaultClassModel(clazz);

		return model;
	}

	public void evalMethod(JavaMethod method, String script,
			DefaultClassModel model) throws ScriptException {

		model.addJavaMethod(method);

		System.out.println("method script=" + script);

	}

	public void evalField(JavaField field, String script,
			DefaultClassModel model) throws ScriptException {

		model.addJavaField(field);

		System.out.println("filed script=" + script);

	}

}
